package top.hepingan.java.controller;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.hepingan.java.common.Result;
import top.hepingan.java.entity.AUser;

import top.hepingan.java.service.IUserService;

@RequestMapping("/user")
@RestController
@Slf4j
@RequiredArgsConstructor
public class UserController {
    @Autowired
    private IUserService iUserService;

    @PostMapping("/login")
    public Result login(@RequestBody AUser user){
        int  id = iUserService.login(user.getUsername(),user.getPassword());
        if ( id!=0){
            StpUtil.login(id);
            return Result.data(StpUtil.getTokenInfo());
        }else {
            return Result.error("登录失败");
        }

    }
    @PostMapping("/register")
    public Result register(@RequestBody AUser user){
      return   iUserService.registr(  user);

    }

}
